Language features
We will modify our compiler, to add the following features.
1 An arbitrary number of print statements
The programmer will be able to print an arbitrary number of string constants
2 Support for input
The hmc programmer will be able to write
input x;
The program will prompt the end-user to input a number, then store that number in the variable x.
3 Conditionals
The hmc programmer will be able to write something like:
if (<expr>) {
<true-statements>
} else {
<false-statements>
}
<next-statements>
The compiled program will evaluate <expr>. If the result is anything other than 0, the program will execute <true-statements>. If the result is 0, the program will execute <false-statements>. After the if statement, the program will continue on to <next-statements>.
4 Loops
The hmc programmer will be able to write something like:
while (<expr>) {
<loop-body-statements>
}
<next-statements>
The compiled program will evaluate <expr>. If the result is anything other than 0, the program will execute <loop-body-statements>. Then the program will evaluate <expr> again, to determine whether to reenter the loop. When <expr> evaluates to 0, the program will continue on to <next-statements>.
Example
With this new grammar, we could write a factorial program!
input n;
x := 1;
while (n) {
x := x * n;
n := n - 1;
}
printString "Factorial is: ";
printNumber x;
Implementation
The updated grammar appears on the next page. I recommend adding features one-at-a-time, rather than all-at-once.